/* ========================================
 *
 * Copyright YOUR COMPANY, THE YEAR
 * All Rights Reserved
 * UNPUBLISHED, LICENSED SOFTWARE.
 *
 * CONFIDENTIAL AND PROPRIETARY INFORMATION
 * WHICH IS THE PROPERTY OF your company.
 *
 * ========================================
*/
#include <project.h>

#include <stdlib.h>

#define ROWS 4
#define COLS 4
char8 hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

CY_ISR(Detect_Key) {
    //CyDelay(200);
    uint8 row;
    row = Pin_row_Read();
    //UART_UartPutChar(48 + row);
    if(row != 0xF) {
      //UART_UartPutString("Hello Baby! ");
      int8 i=0;
      for(i=0;i<COLS;i++) {
        col_reg_Write(~(0x8>>i));
        row = Pin_row_Read();
        //UART_UartPutChar(48 + row);
    
        switch(row) {
            case 0x7 : UART_UartPutChar(hexaKeys[i][0]);break;
            case 0xb : UART_UartPutChar(hexaKeys[i][1]);break;
            case 0xd : UART_UartPutChar(hexaKeys[i][2]);break;
            case 0xe : UART_UartPutChar(hexaKeys[i][3]);break;
        }
      }
      UART_UartPutString("\n");
      col_reg_Write(0x0);
    }
}

CY_ISR(Detect_IR) {
    UART_UartPutString("Detect IR");
}

CY_ISR(UART_RX) {
    UART_UartPutString("Hello Baby! : ");
    char8 ch;
    ch = UART_UartGetChar();
    UART_UartPutChar(ch);
    UART_UartPutString("\n");
}

uint8 col;

int main()
{
    /* Place your initialization/startup code here (e.g. MyInst_Start()) */

    //uint8 ch;
    
    UART_Start();
    CyDelay(1000);
    UART_UartPutString("Hello World!\n");
    
    isr_key_Start();
    isr_key_StartEx(Detect_Key);
    
    isr_ir_Start();
    isr_ir_StartEx(Detect_IR);
    
    isr_uart_Start();
    isr_uart_StartEx(UART_RX);
    
    CyGlobalIntEnable;
    
    /* CyGlobalIntEnable; */ /* Uncomment this line to enable global interrupts. */
    for(;;)
    {
        /* Place your application code here. */
        //ch = UART_UartGetChar();
        //if(ch != 0u) {
        //  UART_UartPutChar(ch);
        //}
        col = col_reg_Read();
    }
}

/* [] END OF FILE */
